package com.pool.store;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.pool.common.Lists;
import com.pool.common.Record;

/**
 * @author claveld@gmail.com (David Clavel)
 */
public class RecordStore implements Store<Record> {

  protected static final String TABLE = "RECORDS";
  protected static final String TEMPORARY_TABLE = "ALL_RECORDS";
  
  private final Database database;
  private final String tableInUse;

  public RecordStore() {
    this(Database.get(), TEMPORARY_TABLE);
  }
  
  public RecordStore(Database database, String tableToUse) {
    this.database = database;
    this.tableInUse = tableToUse;
  }

  public static RecordStore usingTemporaryTable() {
    return new RecordStore(Database.get(), TEMPORARY_TABLE);
  }

  public static RecordStore usingPermanentTable() {
    return new RecordStore(Database.get(), TABLE);
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#add(com.pool.common.Record)
   */
  public boolean add(Record record) {
    ContentValues values = new ContentValues();
    values.put("Id", record.id);
    values.put("Timestamp", record.timestamp);
    values.put("Player", record.player);
    values.put("ShootName", record.shootId);
    values.put("RemainingBalls", record.remainingBalls);
    database.insert(tableInUse, record.id, values);
    return true;
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#update(com.pool.common.Record)
   */
  public boolean update(Record record) {
    ContentValues values = new ContentValues();
    values.put("ShootName", record.shootId);
    database.update(tableInUse, values, "Id = '" + record.id + "'", null);
    return true;
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#delete(com.pool.common.Record)
   */
  public boolean delete(Record record) {
    // TODO(claveld): use the third param
    database.delete(tableInUse, "Id = '" + record.id + "'", null);
    return true;
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#get(String)
   */  
  public Record get(String id) {
    // TODO(claveld): completely not optimized. Query DB instead.
    for (Record record : getAll()) {
      if (record.id.equals(id)) {
        return record;
      }
    }
    return null;
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#getAll()
   */
  public List<Record> getAll() {
    String[] columns = {"Id", "Timestamp", "Player", "ShootName", "RemainingBalls"};
    Cursor cursor = database.query(tableInUse, columns, null, null, null, null, "Id");
    
    int rowsCount = cursor.getCount();
    List<Record> records = Lists.newArrayList(rowsCount);
    cursor.moveToFirst();
    for (int i = 0; i < rowsCount; i++) {
      records.add(new Record(cursor.getString(0),
                             cursor.getString(1),
                             cursor.getString(2),
                             cursor.getString(3),
                             cursor.getInt(4)));
      cursor.moveToNext();
    }
    cursor.close();
    return records;
  }
  
  /* (non-Javadoc)
   * @see com.pool.store.Store#getCount()
   */
  public int getCount() {
    // TODO(claveld): use SELECT COUNT(*) something like that.
    String[] columns = {"Id"};
    Cursor cursor = database.query(tableInUse, columns, null, null, null, null, "Id");
    int count = cursor.getCount();
    cursor.close();
    return count;
  }

  /* (non-Javadoc)
   * @see com.pool.store.Store#getCount()
   */
  public boolean deleteAll() {
    database.delete(tableInUse, "", null);
    return true;
  }
}
